package com.melon.entity;


import org.joda.time.DateTime;

import javax.persistence.*;
import java.util.Date;

/**
 * Created by zhiqi.shao on 2017/3/15.
 */
public class BaseEntity {


    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time")
    private Date createdTime;


    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_modified_time")
    private Date lastModifiedTime;




    public DateTime getCreatedTime() {
        return null == createdTime ? null : new DateTime(createdTime);
    }

    public void setCreatedTime(DateTime createdDate) {
        this.createdTime = null == createdDate ? null : createdDate.toDate();
    }

    public DateTime getLastModifiedTime() {
        return null == lastModifiedTime ? null : new DateTime(lastModifiedTime);
    }

    public void setLastModifiedTime(DateTime lastModifiedDate) {
        this.lastModifiedTime = null == lastModifiedDate ? null
                : lastModifiedDate.toDate();
    }

    @PrePersist
    public void createdDateAudit(Object entity) {
        DateTime createDate = new DateTime();
        ((TimeAuditable) entity).setCreatedDate(createDate);
        ((TimeAuditable) entity).setLastModifiedDate(createDate);
    }

    @PreUpdate
    public void lastModifiedDateAudit(Object entity) {
        ((TimeAuditable) entity).setLastModifiedDate(new DateTime());
    }


}
